package rabbitcore.rabbit.TimeOutQueue;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author zxx
 * @desc
 * @createTime 2021-05-18-10:06
 */
@Component
public abstract class MsgProducer<T> {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(T msgObj, TimeUnit timeUnit, long expiration) {
        Message message = MessageBuilder.withBody(JSON.toJSONString(msgObj).getBytes())
                .setExpiration(String.valueOf(timeUnit.toMillis(expiration)))
                .setTimestamp(new Date())
                .setContentType("application/json")
                .build();
        rabbitTemplate.send(BindDelayConfig.waiteExchange, BindDelayConfig.waiteKey, message, new CorrelationData(UUID.randomUUID().toString()));
    }
}
